package cn.whdreamblog.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import cn.ccit.model.BaseModel;
import cn.whdreamblog.entites.BlogArticle;
import cn.whdreamblog.entites.PageBean;
import cn.whdreamblog.iservers.IBlogArticleServers;
import cn.whdreamblog.servers.BlogArticleServers;

import com.opensymphony.xwork2.ModelDriven;

public class BlogArticleAction extends BaseAction implements ModelDriven<BlogArticle>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//测试第二次提交
	private JSONArray root=null;
	private BlogArticle bArticle=new BlogArticle();
	private IBlogArticleServers ibServers=new BlogArticleServers();
	private String msg="";
	private BlogArticle temp=new BlogArticle();
	private int length = 0;//文字长度
	private String attr=null;
	/**
	 * 获取一定文章数量，可限制文字数目
	 * @return
	 */
	public String requestAllBlogs() {
		try {
			PageBean pageBean  = null;
			if(length>0){
				pageBean = ibServers.getAllBlogSomeArticles(page, limit,length);
			}else{
				pageBean = ibServers.getAllBlogSomeArticlesByPage(limit, page);
			} 
			sendJsonData(pageBean!=null,new BaseModel<PageBean>(), pageBean);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("获取一定文章数量requestAllBlogs:",e);
		}
		return SUCCESS;
	}
	public String searchBlogArticleById() {
		try {
			
			List<BlogArticle> blogList=new ArrayList<BlogArticle>();
			Map<String, Object> datas=ibServers.getBlogArticleAndComments(bArticle.getId());
			BaseModel<Map<String, Object>> data = new BaseModel<Map<String, Object>>();
			data.setData(datas);
			sendJsonDataMap(datas, data);
		} catch (Exception e) {
			LOG.error("searchBlogArticleById:",e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String searchBlogArticleByKind() {
		try {
		
			PageBean pageBean=ibServers.searchBlogArticleByKind(bArticle.getKind(),limit,page);
			BaseModel<PageBean> data = new BaseModel<PageBean>();
			data.setData(pageBean);
			sendJsonData(pageBean!=null,new BaseModel<PageBean>(), pageBean);
		} catch (Exception e) {
			LOG.error("searchBlogArticleByKind:",e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
		
	public String updateBlogArticle() {
		try {
			if(!temp.equals(bArticle)){
				if(ibServers.updateBlogArticle(bArticle)){
					msg="更新成功";
				}else{
					msg="更新失败";
				}
				temp=bArticle;
			}else{
				msg="更新失败";
			}
			
		} catch (Exception e) {
			LOG.error("updateBlogArticle:",e);
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	public String addBlogArticle() {
		try {
			if(ibServers.addBlogArticle(bArticle)){
				msg="添加成功";
			}else{
				msg="添加失败";
			}
			
		} catch (Exception e) {
			LOG.error("addBlogArticle:",e);
			e.printStackTrace();
		}
		
		return SUCCESS;
		
	}
	public String removeBlogArticle() {
		try {
			if(ibServers.removeBlogArticle(bArticle)){
				msg="删除成功";
			}else{
				msg="删除失败";
			}
			
		} catch (Exception e) {
			LOG.error("removeBlogArticle:",e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 通过属性可获取对应文章内容
	 * @return
	 */
	public String requestSomeBlogByAttr() {
		try {
			
			List<BlogArticle> blogList=ibServers.getSomeBlogArticlesAttribute(startId, limit, attr);
			BaseModel<List<BlogArticle>> data = new BaseModel<List<BlogArticle>>();
			data.setData(blogList);
			sendJsonDataList(data);
		} catch (Exception e) {
			LOG.error("requestSomeBlogByAttr:",e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	
	public JSONArray getRoot() {
		return root;
	}
	public void setRoot(JSONArray root) {
		this.root = root;
	}
	@Override
	public BlogArticle getModel() {
		
		return bArticle;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public String getAttr() {
		return attr;
	}
	public void setAttr(String attr) {
		this.attr = attr;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	
}
